package com.epfresh.supplier.activity;

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import com.epfresh.supplier.constant.Constant;
import com.epfresh.supplier.entity.OrderMinusEntity;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class OrderMinusDetailActivity extends BaseActivity {

    static final String TAG = "OrderMinusDetailActivity";
    TextView tvState,tvMoneyTop, tvTime, tvReason,tvMoneyWay;
    View tvInfoHint;

    TextView tvStarter, tvType, tvMoneyBottom,tvNumber,tvMinusReason,tvId,tvApplyTime, tvInfo;
    View ivStarter;

    View llSee;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        id = getIntent().getStringExtra("id");
        if(id!=null){
            requestOrderInfo();
        }else {
            T.toast("没收到订单号");
            finish();
        }
    }
    void initView(){
        setContentView(R.layout.activity_order_minis_detail);

        llSee = findViewById(R.id.ll_see);
        llSee.setOnClickListener(this);
        tvState = (TextView)findViewById(R.id.tv_state);
        tvMoneyTop = (TextView)findViewById(R.id.tv_money_top);
        tvTime = (TextView)findViewById(R.id.tv_time);
        tvReason = (TextView)findViewById(R.id.tv_reason);
        tvMoneyWay = (TextView)findViewById(R.id.tv_money_way);

        tvStarter = (TextView)findViewById(R.id.tv_starter);
        ivStarter = findViewById(R.id.iv_starter);

        tvType = (TextView)findViewById(R.id.tv_type);
        tvMoneyBottom = (TextView)findViewById(R.id.tv_money_bottom);
        tvNumber = (TextView)findViewById(R.id.tv_number);
        tvMinusReason = (TextView)findViewById(R.id.tv_minus_reason);
        tvId = (TextView)findViewById(R.id.tv_id);
        tvApplyTime = (TextView)findViewById(R.id.tv_apply_time);
        tvInfo = (TextView)findViewById(R.id.tv_info);
        ivBack.setVisibility(View.VISIBLE);

        tvInfoHint = findViewById(R.id.tv_info_hint);

        tvReason.setOnClickListener(this);
        tvMoneyWay.setOnClickListener(this);
        ivStarter.setOnClickListener(this);
        rightbtn.setOnClickListener(this);
        rightbtn.setText("帮助");
        rightbtn.setVisibility(View.VISIBLE);
        toolbarTitle.setText("减免退详情");
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_see:
                Intent storeIntent = new Intent(mContext,
                        OrderMinusRecordActivity.class);
                storeIntent.putExtra("id", data.getOrderId());
                startActivity(storeIntent);
                break;
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            case R.id.tv_reason:
                Intent reason;
                reason = new Intent(this, FailReasonActivity.class);
                reason.putExtra("reason",data.getJmtApplyReasonDetails());
                reason.putExtra("phone",data.getJmtApplicantPhone());
                startActivity(reason);
                break;
            case R.id.rightbtn:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra(Constant.KEY_WEB_URL, Constant.URL_HELP_REDUCTION);
                intent.putExtra(Constant.KEY_WEB_TITLE, getString(R.string.help));
                startActivity(intent);
                break;
            case R.id.iv_starter:
                Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getJmtApplicantPhone()));//Uri.parse("tel:" + "4000588198"));//调出来拨打页面；
                startActivity(call);
                break;
            case R.id.tv_money_way:
                Intent detail;
                detail = new Intent(this, OrderMinusMoneyInfoActivity.class);
                detail.putExtra("id",data.getId());
                startActivity(detail);
                break;
        }
    }


    OnRequestListener<OrderMinusEntity> onRequestListener = new OnRequestListener<OrderMinusEntity>() {
        @Override
        public OrderMinusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, OrderMinusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            showExceptionView(R.mipmap.upload_error, "服务器出现小错误，请稍后再试", HttpRequest.FAIL_NET_FAULT);
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            showExceptionView(R.mipmap.upload_error, arg + "", HttpRequest.FAIL_NET_FAULT);
        }
        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<OrderMinusEntity> response, Object tag) {
            OrderMinusEntity responseElement = response.getResponseElement();
            updateOrderInfo(responseElement);
            hideProgressDialog();
        }
    };

    public void requestOrderInfo() {
        RequestEntitySupply<Map<String, String>> entity = new RequestEntitySupply<>();
        entity.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_jmt_detail);
        Map<String, String> map = new HashMap<>();
        map.put("jmtId",id);
        entity.setParameters(map);
        request(entity, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_order_supplier_jmt_detail, onRequestListener);
    }
    OrderMinusEntity data;

    public void updateOrderInfo(OrderMinusEntity data) {
        this.data = data;
        String jmtType = "退款";
        if(data.getJmtType()==2){
            jmtType = "减免";//1:退款；2:减免
            tvMoneyTop.setText(jmtType+"金额： "+data.getJmtTotalPrice()+"元");
            findViewById(R.id.tv_part).setVisibility(View.GONE);
        }else {
            tvMoneyTop.setText(jmtType+"金额： "+data.getRefundPrice()+"元");
            if(data.getJmtRefundChannelTitle()==null||data.getJmtRefundChannelTitle().equals("")||data.getJmtRefundChannelTitle().equals("null")){
                findViewById(R.id.tv_part).setVisibility(View.GONE);
            }else {
                ((TextView) findViewById(R.id.tv_part)).setText("退款渠道：" + data.getJmtRefundChannelTitle());
                findViewById(R.id.tv_part).setVisibility(View.VISIBLE);
            }
        }
        tvState.setText(data.getJmtRefundStatusTitle());
        if(data.getRefundDate()==null||data.getRefundDate().equals("")||data.getRefundDate().equals("null")) {
            tvTime.setVisibility(View.GONE);
        }else{
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText(jmtType + "时间： " + data.getRefundDate());
        }
        tvStarter.setText("发起方：  "+data.getJmtApplicantName());
        tvType.setText("减免退类型："+data.getJmtSubTypeTitle());
        tvMoneyBottom.setText("减免退金额："+data.getJmtTotalPrice()+"元");
        if(data.getJmtProductCount()==null||data.getJmtProductCount()==0){
            tvNumber.setVisibility(View.GONE);
        }else {
            tvNumber.setText("退货数量：" + data.getJmtProductCount()+data.getProductChargeUnit());
            tvNumber.setVisibility(View.VISIBLE);
        }
        tvMinusReason.setText("减免退原因："+data.getJmtAppleReasonTypeTitle());
        tvId.setText("减免退编号："+data.getJmtNo());
        tvApplyTime.setText("申请时间："+data.getJmtApplyDate());
        if(data.getJmtApplyReasonDetails()==null||data.getJmtApplyReasonDetails().equals("")||data.getJmtApplyReasonDetails().equals("null")){
            tvInfoHint.setVisibility(View.GONE);
            tvInfo.setVisibility(View.GONE);
        }else {
            tvInfo.setText(data.getJmtApplyReasonDetails());
            tvInfoHint.setVisibility(View.VISIBLE);
            tvInfo.setVisibility(View.VISIBLE);
        }//1,店铺；2，e批生鲜；3，配送中心；4，采购商
        if(data.getJmtOperatorType()==1||data.getJmtOperatorType()==3||data.getJmtOperatorType()==4){
            ivStarter.setVisibility(View.GONE);
        }else{
            ivStarter.setVisibility(View.VISIBLE);
        }
//1待退款,3退款中,5退款失败,7退款成功,9减免成功
        switch(data.getJmtRefundStatus()){
            case 1:
            case 3:
               /* tvReason.setVisibility(View.GONE);
                tvMoneyWay.setVisibility(View.GONE);*/
                break;
            case 5:
                tvReason.setVisibility(View.GONE);
                tvState.setTextColor(Color.RED);
                break;
            case 7:
                //tvMoneyWay.setVisibility(View.VISIBLE);
                tvState.setTextColor(Color.RED);
                break;
            case 9:
                tvState.setTextColor(Color.RED);
                break;
            default:
                tvReason.setVisibility(View.GONE);
                tvMoneyWay.setVisibility(View.GONE);
                break;

        }
    }
}
